package com.example.springboot.controller;

import com.example.springboot.domain.Admin;
import com.example.springboot.domain.User;
import com.example.springboot.dto.AdminDTO;
import com.example.springboot.dto.AdminDTO2;
import com.example.springboot.dto.LoginDTO;
import com.example.springboot.enums.ResCodeEnum;
import com.example.springboot.exception.NotFoundException;
import com.example.springboot.mapper.AdminMapper;
import com.example.springboot.services.AdminService;
import com.example.springboot.utils.JwtUtil;
import com.example.springboot.vo.ResVO;
import io.jsonwebtoken.Claims;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(value = "user")
public class UserController {

    @Autowired
    private AdminService adminService;

    @PostMapping(value = "login")
    public ResVO<AdminDTO> login(@RequestBody LoginDTO loginDTO) {
        AdminDTO adminDTO=new AdminDTO();
        // 验证数据信息
        if (loginDTO == null || loginDTO.getUsername() == null || loginDTO.getPassword() == null) {
            throw new NotFoundException(5003, "信息输入有误");
        }

        //核对用户名密码
        adminDTO=adminService.hasUser(loginDTO);
        if (adminDTO == null) {
            return new ResVO<AdminDTO>(1002, "用户名或密码错误", adminDTO);
        }

        //响应请求
        return new ResVO<>(5001, "登录成功", adminDTO);
    }

    @PostMapping(value = "/addAdmin")
    public ResVO<String> addAdmin(@RequestBody AdminDTO2 adminDTO){
        Boolean flag = false;
        if( adminDTO == null ){
            return new ResVO<>(ResCodeEnum.ADDER.getResCode(),"添加失败","");
        }

        flag = adminService.addAdmin(adminDTO);

        if(flag){
            return new ResVO<>(ResCodeEnum.ADDOK.getResCode(),"添加成功","");
        }
        return new ResVO<>(ResCodeEnum.ADDER.getResCode(),"用户名已存在","");
    }

    @RequestMapping(value = "/loginFail")
    public ResVO<String> loginFail(){

        log.warn("---------------登录失败退出----------");
        return new ResVO<String>(ResCodeEnum.LOGINER.getResCode(),"用户未登录","");
    }
}
